package org.example.cdpcrpbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.example.cdpcrpbackend.controller.dto.DepStu1;
import org.example.cdpcrpbackend.controller.dto.Department1;
import org.example.cdpcrpbackend.controller.dto.StudentDepartment;
import org.example.cdpcrpbackend.domain.Department;
import org.example.cdpcrpbackend.domain.Student;
import org.example.cdpcrpbackend.mapper.DepartmentMapper;
import org.example.cdpcrpbackend.service.DepartmentService;
import org.example.cdpcrpbackend.util.MyType;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    private final DepartmentMapper departmentMapper;

    @Override
    public StudentDepartment login(String username, String password) {
        // 当前学号和部门管理员账号密码是否匹配
        Student student = departmentMapper.login(username, password);
        if (student == null) {
            return null;
        }

        // 通过查询部门表中的学生管理员（部长）学号，获取部门信息
        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Department::getDepartmentAdminStudentId, student.getId());
        Department department = departmentMapper.selectOne(wrapper);

        StudentDepartment info = new StudentDepartment();
        // 清除密码（重要）
        student.setPassword("");
        info.setStudent(student);
        info.setDepartment(department);
        info.setRole(MyType.RoleDepartment);
        return info;
    }

    @Override
    public List<Department1> listByPage(String key, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return departmentMapper.listByCondition(key);
    }

    @Override
    public Department1 getDepartmentInfoById(String id) {
        return departmentMapper.getDepartmentInfoById(id);
    }

    @Override
    public List<DepStu1> depStuListByPage(String key, String depId, Integer size, Integer page) {
        PageHelper.startPage(page, size);
        return departmentMapper.depStuListByCondition(key, depId);
    }
}
